<template>
  <page-layout :show-back="true">
	  <view class="collections-page">

		<!-- 收藏内容列表 -->
		<scroll-view scroll-y class="collections-list" @scrolltolower="loadMore" refresher-enabled 
			:refresher-triggered="isRefreshing" @refresherrefresh="onRefresh">
			<view v-if="collectionsList.length > 0" class="list-content">
				<view v-for="(item, index) in collectionsList" :key="index" class="collection-item" @tap="goToPostDetail(item.id)">
					<view class="collection-header">
						<view class="author-info">
							<image :src="item.author.avatar" mode="aspectFill" class="author-avatar"></image>
							<text class="author-name">{{item.author.nickname}}</text>
						</view>
						<view class="collection-actions">
							<uni-icons type="star-filled" size="20" color="#FFBB12" @tap.stop="removeCollection(index)"></uni-icons>
						</view>
					</view>
					<view class="post-content">
						<view class="post-text">
							<text class="post-title" v-if="item.title">{{item.title}}</text>
							<text class="post-desc">{{item.content}}</text>
						</view>
						<image v-if="item.cover" :src="item.cover" mode="aspectFill" class="post-cover"></image>
					</view>
					<view class="post-footer">
						<view class="interaction-item">
							<uni-icons type="eye" size="16" color="#999999"></uni-icons>
							<text>{{item.viewCount}}</text>
						</view>
						<view class="interaction-item">
							<uni-icons type="chatbubble" size="16" color="#999999"></uni-icons>
							<text>{{item.commentCount}}</text>
						</view>
						<view class="interaction-item">
							<uni-icons type="heart" size="16" color="#999999"></uni-icons>
							<text>{{item.likeCount}}</text>
						</view>
						<text class="collection-time">{{item.collectionTime}}</text>
					</view>
				</view>
			</view>
			
			<!-- 空状态 -->
			<view v-if="collectionsList.length === 0 && !isLoading" class="empty-state">
				<uni-icons type="star" size="64" color="#cccccc"></uni-icons>
				<text class="empty-text">暂无收藏内容</text>
				<button class="discover-btn" @tap="goToDiscover">去发现精彩内容</button>
			</view>
			
			<!-- 加载状态 -->
			<view v-if="isLoading" class="loading-more">
				<text>加载中...</text>
			</view>
			<view v-if="!isLoading && collectionsList.length > 0 && !hasMore" class="no-more">
				<text>没有更多了</text>
			</view>
		</scroll-view>
	</view>
  </page-layout>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import uniIcons from '@dcloudio/uni-ui/lib/uni-icons/uni-icons.vue';
import PageLayout from "../../components/PageLayout.vue";

// 页面数据
const collectionsList = ref([]);
const page = ref(1);
const pageSize = ref(10);
const hasMore = ref(true);
const isLoading = ref(true);
const isRefreshing = ref(false);

// 加载收藏列表
const loadCollectionsList = async (isRefresh = false) => {
	if (isRefresh) {
		page.value = 1;
		hasMore.value = true;
		collectionsList.value = [];
	}
	
	isLoading.value = true;
	
	// 模拟请求延迟
	setTimeout(() => {
		// 模拟接口返回数据
		const mockPosts = Array.from({ length: 10 }, (_, i) => {
			const id = i + 1 + (page.value - 1) * 10;
			const hasTitle = Math.random() > 0.3;
			const hasImage = Math.random() > 0.4;
			
			return {
				id: id,
				title: hasTitle ? `这是一个很棒的帖子标题 #${id}` : '',
				content: `这是帖子的内容，分享了一些有趣的事情和想法。这是第${id}条内容，希望大家喜欢！${hasTitle ? '' : '这条内容没有标题，但是内容很精彩。'}`,
				cover: hasImage ? `https://picsum.photos/400/300?${id}` : '',
				viewCount: Math.floor(Math.random() * 1000) + 10,
				likeCount: Math.floor(Math.random() * 200) + 10,
				commentCount: Math.floor(Math.random() * 50) + 1,
				collectionTime: `${Math.floor(Math.random() * 10) + 1}天前`,
				author: {
					id: Math.floor(Math.random() * 100) + 1,
					nickname: `创作者${Math.floor(Math.random() * 100) + 1}`,
					avatar: `https://picsum.photos/100?${Math.floor(Math.random() * 100) + 1}`
				}
			};
		});
		
		// 模拟没有更多数据
		if (page.value >= 3) {
			hasMore.value = false;
		}
		
		if (isRefresh) {
			collectionsList.value = mockPosts;
			isRefreshing.value = false;
		} else {
			collectionsList.value = [...collectionsList.value, ...mockPosts];
		}
		
		isLoading.value = false;
	}, 1000);
};

// 加载更多
const loadMore = () => {
	if (!hasMore.value || isLoading.value) return;
	page.value++;
	loadCollectionsList();
};

// 下拉刷新
const onRefresh = () => {
	isRefreshing.value = true;
	loadCollectionsList(true);
};

// 跳转到帖子详情
const goToPostDetail = (postId) => {
	uni.navigateTo({
		url: `/pages/post/detail?id=${postId}`
	});
};

// 取消收藏
const removeCollection = (index) => {
	uni.showModal({
		title: '取消收藏',
		content: '确定要取消收藏该内容吗？',
		success: (res) => {
			if (res.confirm) {
				// 模拟取消收藏请求
				collectionsList.value.splice(index, 1);
				uni.showToast({
					title: '已取消收藏',
					icon: 'success'
				});
			}
		}
	});
};

// 返回上一页
const goBack = () => {
	console.log('返回按钮被点击');
	uni.showToast({
		title: '正在返回...',
		icon: 'none',
		duration: 300
	});
	
	setTimeout(() => {
		uni.navigateBack({
			delta: 1,
			fail: () => {
				uni.switchTab({
					url: '/pages/user/me'
				});
			}
		});
	}, 200);
};

// 去发现页面
const goToDiscover = () => {
	uni.switchTab({
		url: '/pages/index/index'
	});
};

// 页面加载时获取数据
onMounted(() => {
	loadCollectionsList();
});
</script>

<style>
.collections-page {
	background-color: #f8f8f8;
	min-height: 100vh;
}

.collections-list {
	height: calc(100vh - 44px);
}

.list-content {
	padding: 10px 15px;
}

.collection-item {
	margin-bottom: 12px;
	background-color: #ffffff;
	padding: 15px;
	border-radius: 8px;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
}

.collection-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 10px;
}

.author-info {
	display: flex;
	align-items: center;
}

.author-avatar {
	width: 30px;
	height: 30px;
	border-radius: 50%;
	margin-right: 8px;
}

.author-name {
	font-size: 14px;
	color: #333;
	font-weight: 500;
}

.collection-actions {
	display: flex;
	align-items: center;
}

.post-content {
	display: flex;
	margin-bottom: 15px;
}

.post-text {
	flex: 1;
	margin-right: 10px;
}

.post-title {
	font-size: 16px;
	font-weight: 600;
	color: #333;
	margin-bottom: 5px;
	display: block;
}

.post-desc {
	font-size: 14px;
	color: #666;
	line-height: 1.5;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 3;
	overflow: hidden;
}

.post-cover {
	width: 100px;
	height: 80px;
	border-radius: 4px;
	object-fit: cover;
}

.post-footer {
	display: flex;
	border-top: 1px solid #f0f0f0;
	padding-top: 10px;
	align-items: center;
}

.interaction-item {
	display: flex;
	align-items: center;
	margin-right: 15px;
}

.interaction-item text {
	font-size: 12px;
	color: #999;
	margin-left: 4px;
}

.collection-time {
	font-size: 12px;
	color: #999;
	margin-left: auto;
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding-top: 100px;
}

.empty-text {
	font-size: 16px;
	color: #999;
	margin: 20px 0;
}

.discover-btn {
	background-color: #0078ff;
	color: white;
	font-size: 16px;
	padding: 10px 20px;
	border-radius: 100px;
	border: none;
	width: 200px;
}

.loading-more, .no-more {
	text-align: center;
	padding: 20px 0;
	color: #999;
	font-size: 14px;
}
</style> 